/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.selection;

import java.util.HashSet;
import java.util.Set;

import junit.framework.TestCase;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.definition.ConceptTypeDefinition;

public class SelectionManagerTest extends TestCase {

	protected void setUp() throws Exception {
		m_prototypeA = new Query("A", new ConceptTypeDefinition(1, "go_term"));
		m_prototypeB = new Query("B", new ConceptTypeDefinition(1, "go_term"));
		m_prototypeC = new Query("C", new ConceptTypeDefinition(1, "go_term"));
		m_prototypeD = new Query("D", new ConceptTypeDefinition(1, "go_term"));
		SelectionManager.getInstance().clear();
	}
	
	protected void tearDown() throws Exception {
	}
	
	
	public void testAdd() throws Exception {
		assertTrue(SelectionManager.getInstance().size(Query.class) == 0);
        Set<Object> a = new HashSet<Object>();
        a.add(m_prototypeA);
		SelectionManager.getInstance().xorSet(Query.class,a);

        assertTrue(SelectionManager.getInstance().getSelection(Query.class).iterator().next() == m_prototypeA);
		assertTrue(SelectionManager.getInstance().size(Query.class) == 1);
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeA));
        assertTrue(SelectionManager.getInstance().size() == 1);

        Set<Object> b = new HashSet<Object>();
        b.add(m_prototypeB);
		SelectionManager.getInstance().xorSet(Query.class,b);
		
		assertTrue(SelectionManager.getInstance().size(Query.class) == 2);
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeB));
		
		SelectionManager.getInstance().clear();
		assertTrue(SelectionManager.getInstance().size(Query.class) == 0);
		
		Set<Object> set1 = new HashSet<Object>();
		set1.add(m_prototypeA);
		set1.add(m_prototypeB);
		set1.add(m_prototypeC);
		
		SelectionManager.getInstance().xorSet(Query.class,set1);
		
		assertTrue(SelectionManager.getInstance().size(Query.class) == 3);
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeA));
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeB));
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeC));
        assertTrue(SelectionManager.getInstance().size() == 3);
		
		set1.clear();
		set1.add(m_prototypeD);
		
		SelectionManager.getInstance().xorSet(Query.class,set1);
        
		assertTrue(SelectionManager.getInstance().size(Query.class) == 4);
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeD));
	}
	
	public void testRemove() throws Exception {
		assertTrue(SelectionManager.getInstance().size(Query.class) == 0);
		Set<Object> set1 = new HashSet<Object>();
		set1.add(m_prototypeA);
		set1.add(m_prototypeB);
		set1.add(m_prototypeC);
		
		SelectionManager.getInstance().xorSet(Query.class,set1);
		
		assertTrue(SelectionManager.getInstance().size(Query.class) == 3);
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeA));
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeB));
		assertTrue(SelectionManager.getInstance().contains(Query.class,m_prototypeC));
		
	
        Set<Object> a = new HashSet<Object>();
        a.add(m_prototypeA);
		SelectionManager.getInstance().xorSet(Query.class,a);
		assertTrue(SelectionManager.getInstance().size(Query.class) == 2);
		assertFalse(SelectionManager.getInstance().contains(Query.class,m_prototypeA));

	}
	
	public void testFilter() throws Exception {
		assertTrue(SelectionManager.getInstance().size(Query.class) == 0);
		Set<Object> set1 = new HashSet<Object>();
		
		set1.add(m_prototypeA);
		set1.add(m_prototypeB);
		SelectionManager.getInstance().setSet(Query.class,set1);
		
		// The filtered selection should return 3 items
		// if filtered for a QueryNode, 1 item for a ConceptronTypeNode
		// and 2 items for a ConceptPrototypeNode
		assertTrue(SelectionManager.getInstance().size(Query.class) == 2);
		assertTrue(SelectionManager.getInstance().getDefinitionFilteredSelection(ConceptTypeDefinition.class).size() == 2);
		assertTrue(SelectionManager.getInstance().getDefinitionFilteredSelection(ConceptTypeDefinition.class).contains(m_prototypeA));
		assertTrue(SelectionManager.getInstance().getDefinitionFilteredSelection(ConceptTypeDefinition.class).contains(m_prototypeB));
		
		
		
	}
    
    // TODO: Add tests for canselect
	
	Query m_prototypeA,m_prototypeB,m_prototypeC,m_prototypeD;
	
}
